package javaTec.BueatyOfMath;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

public class WordCount {

    public static void main(String[] args) {
        File file = new File("C:\\Users\\yangshunxing\\Desktop\\file\\test.txt");
        try {
            BufferedReader bf = new BufferedReader(new FileReader(file));
            StringBuffer stringBuffer = new StringBuffer();
            Map<String, Integer> wordCount = new HashMap<>();
            String str = "";
            while (null != (str = bf.readLine())) {
                if (!" ".equals(str)) {
                    stringBuffer.append(str);
                }
            }
            List<String> allChar = new ArrayList<>();
            for (int i = 0; i < stringBuffer.toString().length(); i++){
                allChar.add(stringBuffer.substring(i,i+1));
            }
            for (String singleChar: allChar){
                if (wordCount.get(singleChar) != null){
                    Integer count = wordCount.get(singleChar);
                    count++;
                    wordCount.put(singleChar,count);
                } else {
                    wordCount.put(singleChar,1);
                }
            }

            Map<Integer, Integer> nr = new HashMap<>();
            for (Integer num: wordCount.values()){
                if (nr.get(num) != null){
                    Integer count = nr.get(num);
                    count++;
                    nr.put(num,count);
                } else {
                    nr.put(num,1);
                }
            }
            Integer count = 0;
            nr.keySet().stream().forEach(singleChar -> {
                System.out.print(singleChar + "---" + nr.get(singleChar)+";");
            });
            for (Integer cishu: nr.keySet()){
                count = count + nr.get(cishu)*cishu;
            }
            System.out.println();
            System.out.println(count);
            System.out.println(allChar.size());
        } catch (IOException e){
            
        }
    }

    /**
     * 显示输入流中还剩的字节数
     *
     * @param in
     */
    private static void showAvailableBytes(InputStream in) {
        try {
            System.out.println("当前字节输入流中的字节数为:" + in.available());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
